<template>
    <div>
        <el-calendar v-model="date">
            <template
            slot="dateCell"
            slot-scope="{data}">
             <div class="calendar-day">
                    {{
                    data.day
                    .split('-')
                    .slice(2)
                    .join('-')
                    }}
                  </div>

                 <div ref="rc">
                <div v-for="(item,index) in getItem(data.day).arr" :key="index" class="item">
                        <div class="item-title" v-if="isShow(data.day,item.startDate)" @click="showDetail(item.id)" :style="'background:'+colors[item.level-1]+';width:'+((getWidth(item,data.day) * 100)+95)+'%'">
                        <span class="time">
                            {{item.startTime
                                .split(":").slice(0,2).join(':')
                                }}
                            </span>{{item.title}}
                        </div>
                    </div>
                    <div v-if="getItem(data.day).length>0" class="item-all" @click="showAllItem4Day(data.day)">剩余{{getItem(data.day).length}}个,查看全部</div>
                </div>
            </template>
        </el-calendar>

         <el-dialog :title="title" :visible.sync="showDetails" width="40%" append-to-body>
             <Details :id="itemId"></Details>
         </el-dialog>
        
         <el-dialog :title="title" :visible.sync="showAll" width="20%" append-to-body>
            <div v-for="(item,index) in allItem" :key="index" :style="'background:'+colors[item.level-1]" class="detaial-item" @click="showDetail(item.id)">
                 <span class="time">
                           {{item.startTime
                            .split(":").slice(0,2).join(':')
                             }}—
                             {{item.endTime
                            .split(":").slice(0,2).join(':')
                             }}
                        </span>{{item.title}}
            </div>
        </el-dialog>


    </div>
</template>

<script>
import Details from "./Details.vue"
import moment from "moment";
export default {
    name:'Calendar',
    props:["datas"],
    components:{
        Details
    },
    data(){
        return {
            date:new Date(),
             //颜色，依次代表(1一般,2重要，3紧急)
            colors:['#ff9800','#3f51b5','#2196f3'],
            itemId:0,
            showDetails:false,
            title:"",
             // 详细数据
            allItem: {},
            showAll:false,
            //当天的所有数据
            allItem:[],
        }
    },
    created(){
            this.$nextTick(() => {
                 // 点击前一个月
                  let prevBtn = document.querySelector(
                ".el-calendar__button-group .el-button-group>button:nth-child(1)"
                );
                prevBtn.addEventListener("click", e => {
                    this.$emit('dateClick',this.date)
                })
                // 点击今天
                let prevBtn2 = document.querySelector('.el-calendar__button-group .el-button-group>button:nth-child(2)');
                prevBtn2.addEventListener('click',() => {
                     this.$emit('dateClick',this.date)
                })
                // 点击下个月
                let prevBtn3 = document.querySelector('.el-calendar__button-group .el-button-group>button:nth-child(3)');
                prevBtn3.addEventListener('click',() => {
                     this.$emit('dateClick',this.date)
                })
           
            })
    },
     computed:{

         //获取当天的所有日程
         getItem(){
            return function(day){
                let arr =[];
                let length=0;
                let list = [];
                for (let index = 0; index < this.datas.length; index++) {
                    if((moment(day) >= moment(this.datas[index].startDate)) && 
                            moment(day) <= moment(this.datas[index].endDate)){
                                
                        list.push(this.datas[index]);
                         if(arr.length<3){
                                 arr.push(this.datas[index]);
                         }else{
                            length += 1;
                         }
                    }
                }
                if(length>0){
                    arr.pop();
                    length += 1;
                }
                return {arr,length,list};
            }
        },

        //是否显示标题
        isShow(){
            return function(day,start){
                return (moment(start).isSame(moment(day),"date") || moment(day).day() ==1) ;
            }
        },

       //计算宽度
        getWidth(){
            return function(item,now){
              //结束时间离开始时间多少天
              let days = moment(item.endDate).diff(moment(item.startDate),'day');
              //用计算出当前开始时间离周天有几天
              let a1 = 7 - moment(item.startDate).day();
              //判断当前时间是否是开始时间
              if(moment(item.startDate).isSame(moment(now),"date")){
                //如果当前总的天数大于离周天的天数，
                if(a1<=days){
                    return a1;
                }else{
                    return days;
                }
              }else{
                  return days-a1-1
              }
            }
        },


     },
     methods:{
         showDetail(id){
             this.showDetails = true;
             this.itemId = id;
             this.title="详情";
         },
          //显示当天的所有数据
        showAllItem4Day(day){
               
            this.allItem = [];
            for (let index = 0; index < this.datas.length; index++) {
                if((moment(day) >= moment(this.datas[index].startDate)) && 
                        moment(day) <= moment(this.datas[index].endDate)){
                    this.allItem.push(this.datas[index]);
                }
            }
            this.title = day;
            this.showAll = true;
        },
     }
}
</script>

<style lang="scss" scoped>

.item{
    height: 18px;
    font-size: 12px;
    margin-bottom: 1px;
}
.item-title{
    height: 18px;
    font-size: 12px;
    position: relative;
    margin-bottom: 2px;
    border-radius: 5px;
    color: white;
    text-align: left;
    padding-left: 4px;
    font-weight: 700 !important;
}
.item-all{
    z-index: 5;
    position: relative;
    background-color: white;
    text-align: center;
    color: rgb(5,113,255);
    font-weight: 700;



}
 ::v-deep .el-calendar-table .el-calendar-day{
    padding: 0;
  }
  .time{
      margin-right: 5px;
      
  }
  .detail{
      padding: 0 40px;
      font-size: 16px;
  }
  .title{
      margin-right: 10px;
      text-align: right;
      width: 40px;
  }
  .detaial-item{
      margin-bottom: 10px;
      padding: 10px;
      color: white;
      font-size: 700;
      cursor: pointer;
  }
</style>